// https://leetcode.cn/problems/shift-2d-grid/
// Created by ade on 2022/7/20.
//

#include <iostream>
#include <vector>


using namespace std;

class Solution {
public:
    vector <vector<int>> shiftGrid(vector <vector<int>> &grid, int k) {
        int row = grid.size(); // 5
        int col = grid[0].size(); // 1
        int len = row * col;
        k = len - k % len;
        if(k == len) return grid;
        vector <vector<int>> res(row, vector<int>(col));
        int i = 0;
        while (i < len) {
            res[i / col][i % col] = grid[k / col][k % col];
            i++;
            k++;
            if (k >= len) k = k - len;
        }
        return res;
    }

    void show(vector <vector<int>> &grid) {
        for (auto items:grid) {
            for (auto i:items) {
                cout << i << ",";
            }
            cout << endl;
        }
    }
};

int main() {
    vector <vector<int>> grid = {{1},
                                 {2},
                                 {3},
                                 {5},
                                 {4}};
    Solution so;
    auto res = so.shiftGrid(grid, 1);
    so.show(res);
    return 0;
}
